define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'sendgoods_service/index' + location.search,
                    add_url: 'sendgoods_service/add',
                    edit_url: 'sendgoods_service/edit',
                    del_url: 'sendgoods_service/del',
                    multi_url: 'sendgoods_service/multi',
                    import_url: 'sendgoods_service/import',
                    use_url: 'sendgoods_service/use',
                    table: 'sendgoods_service',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'user.mobile', title: __('User.mobile'), operate: 'LIKE'},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'usetimestart', title: __('Usetimestart'), addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'usetimeend', title: __('Usetimeend'), addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'reservetime', title: __('Reservetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'usetime', title: __('Usetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'state', title: __('State'), searchList: {"0":__('State 0'),"1":__('State 1'),"2":__('State 2')}, formatter: Table.api.formatter.normal},
                        {field: 'shoprogoods.title', title: __('Shoprogoods.title'), operate: 'LIKE'},
                        {field: 'shoprostore.name', title: __('Shoprostore.name'), operate: 'LIKE'},
                        {field: 'shoprostoreproject.name', title: __('Shoprostoreproject.name'), operate: 'LIKE'},
                        {field: 'shoprostorewaiter.name', title: __('Shoprostorewaiter.name'), operate: 'LIKE'},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('核销操作'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('点击核销'),
                                    title: __('点击核销'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: $.fn.bootstrapTable.defaults.extend.use_url,
                                    confirm: '确认核销',
                                    // success: function (data, ret) {
                                    //     Layer.alert(ret.msg);
                                    //     //如果需要阻止成功提示，则必须使用return false;
                                    //     //return false;
                                    // },
                                    refresh: true,
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});